﻿/*
 * Creato da SharpDevelop.
 * Utente: Ciccio
 * Data: 13/07/2008
 * Ora: 11.11
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */

using System.Data;
using System.Web;
using System;
using ACM.cmsarea08.Administration;

namespace ACM.cmsarea08.Administration.Core
{
	/// <summary>
	/// Description of Class1.
	/// </summary>
	public class PrivatePathWay
	{
		private static PrivatePathWay _istanza=null;
		private static object locker = new object();
		private ACM.cmsarea08.Administration.Core.DBClass dclass;
		
		private int CurrentUrlNumber{
			get {
				if(HttpContext.Current.Session["PrivatePathPosition"] != null) {
					return Convert.ToInt16( HttpContext.Current.Session["PrivatePathPosition"].ToString() );
				} else return 0;
			}
			set {
				HttpContext.Current.Session["PrivatePathPosition"] = value.ToString();
			}
			
		}
		public static PrivatePathWay privatePathWay {
			get {
				if(_istanza==null) {
					lock(locker) { _istanza = new PrivatePathWay(); }
				}
				return _istanza;
			}
		}
		
		
		
		public PrivatePathWay()
		{
			
			
		}
		public void ClearPageHistory() {
			try {
			HttpCookie cc = HttpContext.Current.Request.Cookies["PrivatePathWay"];
			if(cc!=null) cc.Expires = DateTime.Now;
			HttpContext.Current.Response.Cookies.Add(cc);
			HttpContext.Current.Response.Cookies.Remove("PrivatePathWay");
			} catch {}
		}
		public void AddPagetoHistory(KindUrl tipo) {
			string url = HttpContext.Current.Request.Url.ToString();
			string qs  = HttpContext.Current.Request.Url.Query;
			CurrentUrl mUrl = new CurrentUrl(url,qs,tipo);
			int getLast = (this.CurrentUrlNumber)+1;
//			HttpContext.Current.Response.Write("STO PER SALVARE LISTA: " + getLast + " --> " + mUrl.Url + "<br/>");
			HttpCookie cookie =null;
			if(HttpContext.Current.Request.Cookies["PrivatePathWay"]!=null) {
				cookie = HttpContext.Current.Request.Cookies.Get("PrivatePathWay");
			} else {
				cookie =  new HttpCookie("PrivatePathWay");
			}
			cookie.Expires = DateTime.Now.AddHours(2);
			cookie["point_" + getLast] = mUrl.Url.ToString();
			cookie["tipo_" + getLast] = mUrl.Type.ToString();
			HttpContext.Current.Response.Cookies.Add(cookie);
			CurrentUrlNumber = getLast;
		}
		
		public string GetLastListPage() {
			int getLast = this.CurrentUrlNumber;
			string urlfound = "";
			int breakLine = -1;
			for(int i=getLast; i>0; i--) {
				if( HttpContext.Current.Request.Cookies["PrivatePathWay"]==null) break;
				if( HttpContext.Current.Request.Cookies["PrivatePathWay"]["tipo_" + i]==null) { 
					breakLine = i;
					continue;
				}
				if( HttpContext.Current.Request.Cookies["PrivatePathWay"]["tipo_" + i].ToString()=="")  {
					breakLine = i;
					continue;
				}
				//verifica il tipo:
				if( HttpContext.Current.Request.Cookies["PrivatePathWay"]["tipo_" + i].ToString()==KindUrl.ListPage.ToString()) {
					if( HttpContext.Current.Request.Cookies["PrivatePathWay"]["point_" + i]==null) {
						breakLine = i;
						continue;
					}
					if( HttpContext.Current.Request.Cookies["PrivatePathWay"]["point_" + i].ToString()=="") {
						breakLine = i;
						continue;
					}
					urlfound= HttpContext.Current.Request.Cookies["PrivatePathWay"]["point_" + i].ToString();
					breakLine = i;
					break;
				}
			}
			if(breakLine>-1) {
//				HttpContext.Current.Response.Write("AZZERA : " + getLast + " --> " + breakLine + "<br/>");
				HttpCookie cookie =null;
				if(HttpContext.Current.Request.Cookies["PrivatePathWay"]!=null) {
					cookie = HttpContext.Current.Request.Cookies.Get("PrivatePathWay");
				} else {
					cookie =  new HttpCookie("PrivatePathWay");
				}				
				for(int i = getLast; i>=breakLine; i--) {
					//elimina i cookie.
					
					if( cookie["tipo_" + i]==null) {
						cookie["tipo_" + i]="";
					}
					if( cookie["point_" + i]==null) {
						cookie["point_" + i]="";
					}
				}
				this.CurrentUrlNumber = breakLine;
				HttpContext.Current.Response.Cookies.Add(cookie);
			}
			return urlfound;
		}
		
	}
	
	public class CurrentUrl {
		private string url;
		private string querystring;
		private KindUrl tipo;
		public CurrentUrl() {}
		public CurrentUrl(string _url, string _querystring, KindUrl _tipo) {
			url = _url;
			querystring = _querystring;
			tipo = _tipo;
		}
		public string Url { get {return url; } set { url = value; } }
		public string QueryString { get {return querystring; } set { querystring = value; } }
		public KindUrl Type { get {return tipo; } set { tipo = value; } }
		public string ToString() {
			return url + "!~!" + tipo;
		}
	}
	
	public enum KindUrl {
		ListPage,
		EditPage,
		Plugins
	}
}
